package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"lyt_film/models"
	"strconv"
	"time"
)

type FilmController struct {
	beego.Controller
}
type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *FilmController) Get() {
}
func (c *FilmController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	if header.Size >= 5*1024*1024 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件过大,请重新上传",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	path := "static/img/" + header.Filename
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "文件添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		imgSrc := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "文件添加成功",
			Data: imgSrc,
		}
		c.ServeJSON()
	}
}
func (c *FilmController) Add() {
	name := c.GetString("name")
	if name == "" {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "电影名称不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	price, _ := c.GetFloat("price")
	if price <= 0 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "电影票价不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	info := c.GetString("info")
	img := c.GetString("img")
	profile := c.GetString("profile")
	star := c.GetString("star")

	film := models.Film{
		Name:    name,
		Price:   price,
		Info:    info,
		Img:     img,
		Profile: profile,
		Star:    star,
	}
	err := models.DB.Debug().Create(&film).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *FilmController) List() {
	pageDate := make(map[string]interface{})
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	query := models.DB.Debug().Model(&models.Film{})

	name := c.GetString("name")
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}

	key := "name-" + name
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var count int64
		query.Count(&count)
		offset := (page - 1) * size
		var film []models.Film
		err = query.Offset(offset).Limit(size).Find(&film).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			pageDate["count"] = count
			pageDate["film"] = film
			jsonByte, _ := json.Marshal(&pageDate)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Minute*3)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功-mysql",
				Data: pageDate,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageDate)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageDate,
		}
		c.ServeJSON()
	}

}
func (c *FilmController) Delete() {
	id, _ := c.GetInt("id")
	err := models.DB.Debug().Where("id=?", id).Delete(&models.Film{}).Error
	if err != nil || id <= 0 {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *FilmController) Info() {
	id, _ := c.GetInt("id")
	key := "id-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var film models.Film
	if err != nil {
		err = models.DB.Debug().Where("id=?", id).First(&film).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			jsonByte, _ := json.Marshal(&film)
			value := string(jsonByte)
			models.Rdb.Set(key, value, time.Minute*3)
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "查询成功-mysql",
				Data: film,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &film)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: film,
		}
		c.ServeJSON()
	}
}
